//
//  CateCell.m
//  悠狸
//
//  Created by dlios on 15-8-28.
//  Copyright (c) 2015年 Dung. All rights reserved.
//

#import "CateCell.h"
#import "CateModel.h"
#import "CateCollectionViewCell.h"
@interface CateCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain)UICollectionView *collect;
@property (nonatomic, retain)NSMutableArray *array1;
@property (nonatomic, retain)NSMutableArray *arr;
@property(nonatomic ,retain)UILabel *label;

@end
@implementation CateCell

- (void)dealloc
{

    [_label release];
    [_collect release];
    [_model release ];
    [_array1 release];
    [_arr release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.array1 = [NSMutableArray array];
        
        [self create];
    }
    return self;
}

- (void)create
{
  
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(10 *OffWidth, 5*OffHeight, 355*OffWidth, 30*OffHeight)];
    [self.contentView addSubview:_label];
#pragma wark---品类
//    _label.backgroundColor = [UIColor redColor];
    [_label release];
    
     CGFloat width = ([UIScreen mainScreen].bounds.size.width )/ 6;
    
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake(width*OffWidth, 85*OffHeight);
//    layout.minimumInteritemSpacing = 15;
//    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(10*OffWidth, 10*OffHeight, 10, 10*OffHeight);
    self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(10*OffWidth, 40*OffHeight, [UIScreen mainScreen].bounds.size.width-10 , 200*OffHeight) collectionViewLayout:layout];
    self.collect.backgroundColor = [UIColor whiteColor];
    self.collect.delegate =self;
    self.collect.dataSource =self;
    
    [self.contentView addSubview:_collect];
    
 
    [_collect registerClass:[CateCollectionViewCell class] forCellWithReuseIdentifier:@"f"];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    CateCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"f" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    CateModel *model1 = [_array1 objectAtIndex:indexPath.item];
//    cell.backgroundColor = [UIColor purpleColor];
  
    
     
    [cell sendModel:model1];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    CateModel *model = [_array1 objectAtIndex:indexPath.row];
    
    [self.dele sendCate:model];
    
    
}
// jieshou
-(void)sendModel:(CateModel *)model
{
   
    _label.text = model.name;
    _arr = model.channels;
  
    for (int i = 0; i <_arr.count ; i++) {
        CateModel *mod = [[CateModel alloc]init];
        NSDictionary *dic = [_arr objectAtIndex:i];
        [mod setValuesForKeysWithDictionary:dic];
        [_array1 addObject:mod];
        [mod release];
       
    }
    [_collect reloadData];

}
#pragma mark --- 过来先走这个方法,第一个item比较高,其它的低
- (void)layoutSubviews{
    [super layoutSubviews];
    if (_arr.count >10) {
        _collect.frame = CGRectMake(0, 40*OffHeight, [UIScreen mainScreen].bounds.size.width , 300*OffHeight);

    } else {
        _collect.frame = CGRectMake(0, 40*OffHeight, [UIScreen mainScreen].bounds.size.width, 200*OffHeight);
    }
    
}
@end
